// *** SCENE OBJECT *** //
// Controls the main part of the game, and call all the sub methods
//
// @project: GoldenShield
// @author: Mathias Sørensen, Kasper B. Jensen
// @version: 1.0

var scene = {
    windowHeight: $(window).height(), //Browser window height
    windowWidth: $(window).width(),   //Browser window width
    sceneWidth: 800,                  //The game window width
    sceneHeight: 600,                 //The game window height
    updateInterval: 30,               //Refresh rate of the game
    gameLoop: null,                   //Contains the main loop
    gameStart: false,                 //Boolean value that tells if the game has startet
    gameIsOver: false,                //Boolean value that tells if the game is over
    
    init: function() {
      server.getConfig(function(d) {
          if(d) {
              scene.sceneHeight = d.dimensions.hight;
              scene.sceneWidth = d.dimensions.width;
              scene.updateInterval = d.updateInterval;
              scene.startMap = d.startMap;
              scene.plugindir = d.pluginFolder;
              scene.createScene();
          } else {
              return false;
          }
      });  
    },
    
    //Creates and position the scne div that the game runs in.
    createScene: function(){
        //Positioning
        var left = this.windowWidth / 2 - this.sceneWidth / 2;
        var top = this.windowHeight / 2 - this.sceneHeight / 2;
				
        //Appending scene and gui screens
        $('body').append('<div id="scene"></div>');
        $('#scene').append('<div id="loading"></div><div id="hitOverlay"></div><div id="mapContent"></div><div id="guiOverlay"></div><div id="sounds"></div>');
                
        //Adding CSS information
        $('#loading').css({
            position: "absolute", 
            display: "none", 
            width: this.sceneWidth + 'px', 
            height: this.sceneHeight + 'px', 
            backgroundColor: "black", 
            zIndex: "1000"
        });
        $('#hitOverlay').css({
            position: "absolute", 
            width: "800px", 
            height: "600px", 
            display: "none", 
            backgroundImage: "url(img/gui/hitoverlay.png)", 
            zIndex: "200"
        });
        $('#scene').css({
            width: this.sceneWidth + 'px', 
            height: this.sceneHeight + 'px', 
            border: '5px solid #000', 
            left: left + 'px', 
            top: top + "px", 
            position: "absolute"
        });
        $('#guiOverlay').css({
            width: this.sceneWidth + 'px', 
            height: this.sceneHeight + 'px', 
            position: "absolute",  
            display: "none", 
            zIndex: "1000"
        });
                
        //Creates the right menu
        site.createMenu();
        sound.init(); //Init the game sounds
                
        //Create eventlisteners and runs the game
        this.createEventListeners();
        this.showStartScreen();
    },
            
    //Loads the eventlisteners
    createEventListeners: function() {
        //Creates eventlisteners on keyStrokes
        $(document).keydown(function(e){
            movementObject.onKeyDown(e);
        });
                
        $(document).keyup(function(e){
            movementObject.onKeyUp(e);
        });
                
        //Create eventlistener on resize
        $(window).resize(function() {
            scene.updateScene();
        })
    },
            
    //Shows the welcome screen
    showStartScreen: function() {
        $('#loading').css({
            backgroundImage: "url('img/loadingscreen/goldenshield.start2.png')"
        });
        $('#loading').fadeIn();
    },
            
    //Updates the position of the scene on window resize
    updateScene: function() {
        var left = $(window).width() / 2 - this.sceneWidth / 2;
        var top = $(window).height() / 2 - this.sceneHeight / 2;
        $('#scene').css({
            left: left + 'px', 
            top: top + "px"
            });
    },
            
    //Loads a new map into the game
    loading: function(mapLoading) {
        $('#loading').fadeIn(1);
        $('#loading').css({
            backgroundImage: 'url(img/loadingscreen/' + mapLoading + '.loading.png)', 
            backgroundRepeat: "no-repeat"
        });
        character.removeQuests();
        character.resetIfAction();
        character.resetDieAction();
        guiObject.removeMessage();
        player.resetMapKill();
        this.pause();
    },
            
    //Removes the loading screen, when the map has been loaded from the server
    removeLoading: function() {
        character.loadCharecters();
        character.updateDamage();
        character.startShoting();
        sound.playBgSound();
        $('#loading').fadeOut("fast");
        scene.unPause();
        character.runStartupQuest();
    },
            
    //Main game loop function
    update: function() {
        movementObject.update();
        guiObject.updateGui();
                
        //Checks to see if health level is zero!
        if(player.health <= 0) {
            scene.gameOver();
        }
    },
            
    //This function starts the game
    start: function() {
                
        //Tells that the game has started!
        this.gameStart = true;
         
        //Loads the firstmap
        server.loadMap(this.startMap);
                
        //Starts the main gameloop
        this.gameLoop = setInterval(this.update,this.updateInterval);
                
        //Adds the GUI to the game
        guiObject.showGui();
    }, 
            
    //Runs on gameOver
    gameOver: function() {
        scene.pause();
        $('#guiOverlay').css({
            backgroundImage: "url(img/loadingscreen/gameover.gif)"
        });
        $('#guiOverlay').fadeIn("slow");
        $('#guiOverlay').html('');
        scene.gameIsOver = true;
    },
            
            
    //When player dies and presses enter the game restarts
    continueFromGameOver: function() {
        scene.resetGame(); 
        scene.unPause();
        $('#guiOverlay').fadeOut("slow");
        server.loadMap(this.startMap);
        scene.gameIsOver = false;         
    },
            
    //Runs when the player wins the game
    gameWin: function() {
        $('#guiOverlay').css({
            backgroundImage: "url(img/loadingscreen/winscreen.gif)"
        });
        $('#guiOverlay').fadeIn("slow");
        this.showScorebord();
        this.pause();
        scene.gameIsOver = true;  
    },
            
    //Shows the score on GuiOverlay
    showScorebord: function() {
        $('#guiOverlay').append('<div id="yourScore"></div>');
        $('#yourScore').hide();
        $('#yourScore').css({
            color: "#fff", 
            textAlign: "center", 
            fontFamily: 'Courier New', 
            fontWeight: 'bolder', 
            fontSize: '150%', 
            paddingTop: "400px", 
            fontSize: "32px"
        });
                
        var yourScoreHtml = "<p>You have killed: " + player.playerKills + "</p>";
        yourScoreHtml += "<p>You have got: <br/>";
        yourScoreHtml += player.jewels + " - Jewels</p>";
                
        $('#yourScore').html(yourScoreHtml);
        $('#yourScore').fadeIn();               
    },
            
    pause: function() { //pauses the game
        movementObject.stop();
        character.charecterPause();
    },
            
    unPause: function() { //unpauses the game
        movementObject.start();
        character.charecterUnPause();
    },
            
    debugMode: function() { //turns on debug mode
        collision.showPlayerPoints();
        player.debugMode = true;
    },
            
    //Displays the red hit overlay, when player is hit
    onHitAnimation: function() {
        $('#hitOverlay').show();
    
        var func = function() {
            $('#hitOverlay').hide();
        }
        setTimeout(func, 110);
    },
            
    //Resets the different objects back to defualts
    resetGame: function() {                
        character.reset();
        player.reset();          
        collision.resetCollision(); 
    }
}